package com.chijun.mform.process.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chijun.mform.FormBuilder;
import com.chijun.mform.R;
import com.chijun.mform.create.fragment.FormAddProcessFragment;
import com.chijun.mform.full.mode.ModeFormFullApprovePerson;
import com.chijun.mform.process.IHandlerLoadFormFull;
import com.chijun.mform.process.IWaitLoadCompleteListener;
import com.chijun.mform.process.IWaitLoadCompleteResultDataListener;
import com.chijun.mform.process.IWritePingJiaCallBack;
import com.chijun.mform.process.ModeServerFullData;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.b.c.dialog.ErrorDialog;
import com.example.ccj.b.c.dialog.LoadingDialog;
import com.example.ccj.u.ClipboardUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;
import com.example.ccj.u.server.MyObserver;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class MainViewLoadProcessAllData extends LinearLayout {
    ViewLoadFullFieldData loadFullDataView;
    ViewLoadFullProcessData loadFullProcessDataView;
    ViewLoadApprovalControl loadApprovalView;
    TextView formName,formId;
    MTextView copyFormId;
    ImageView formFullStata;
    String formFullId;
    String formFullData;
    ModeServerFullData modeServerFullData;
    IHandlerLoadFormFull iHandlerLoadFormFull;
    IProcessViewApproveOver processViewApproveOver;

    public MainViewLoadProcessAllData(Context context, ModeServerFullData modeServerFullData, IHandlerLoadFormFull iHandlerLoadFormFull) {
        super(context);
        this.modeServerFullData = modeServerFullData;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        init();
    }

    public MainViewLoadProcessAllData(Context context, String formFullData, IHandlerLoadFormFull iHandlerLoadFormFull) {
        super(context);
        this.formFullData = formFullData;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        init();
    }

    public MainViewLoadProcessAllData(Context context, long formFullId, IHandlerLoadFormFull iHandlerLoadFormFull) {
        super(context);
        this.formFullId = String.valueOf(formFullId);
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        init();
    }

    public MainViewLoadProcessAllData(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        LinearLayout.inflate(getContext(), R.layout.view_load_all_form_full_data,this);
        loadFullDataView = findViewById(R.id.view_load_all_form_full_data_filed_data);
        loadFullProcessDataView = findViewById(R.id.view_load_all_form_full_data_process_data);
        loadApprovalView = findViewById(R.id.view_load_all_form_full_data_process_mode);
        formName = findViewById(R.id.view_load_all_form_full_data_form_name);
        formId = findViewById(R.id.view_load_all_form_full_data_form_id);
        formFullStata = findViewById(R.id.view_load_all_form_full_data_state);
        copyFormId = findViewById(R.id.view_load_all_form_full_data_copy_id);
    }


    public MainViewLoadProcessAllData setFormFullId(String formFullId, IHandlerLoadFormFull iHandlerLoadFormFull){
        this.formFullId = formFullId;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        return this;
    }

    public MainViewLoadProcessAllData setFormFullData(String formFullData, IHandlerLoadFormFull iHandlerLoadFormFull){
        this.formFullData = formFullData;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        return this;
    }

    public MainViewLoadProcessAllData setFormFullData(ModeServerFullData modeServerFullData, IHandlerLoadFormFull iHandlerLoadFormFull){
        this.modeServerFullData = modeServerFullData;
        this.iHandlerLoadFormFull = iHandlerLoadFormFull;
        return this;
    }

    /**
     * 设置提交完毕后的回调
     * @param processViewApproveOver 1
     * @return
     */
    public MainViewLoadProcessAllData setUploadOverListener(IProcessViewApproveOver processViewApproveOver) {
        this.processViewApproveOver = processViewApproveOver;
        return this;
    }

    public void builder(){
        analysis();
    }

    private void analysis(){
        LoadingDialog loadingDialog = new LoadingDialog(getContext());
        loadingDialog.showLoading();

        Observable.create((ObservableOnSubscribe<ModeServerFullData>) e -> {
                    if (modeServerFullData != null) {
                        e.onNext(modeServerFullData);
                    } else if (formFullData != null) {
                        modeServerFullData = ModeServerFullData.analysis(formFullData);
                        if (modeServerFullData != null) {
                            e.onNext(modeServerFullData);
                        }else {
                            LogUtils.e("未获取到表单审批数据！");
                            loadingDialog.dismiss();
                            ErrorDialog errorDialog = new ErrorDialog(getContext(),"未获取到表单审批数据");
                            errorDialog.show();
                        }
                    } else if (formFullId != null && iHandlerLoadFormFull != null) {
                        iHandlerLoadFormFull.loadFullData(formFullId, new IWaitLoadCompleteResultDataListener() {
                            @Override
                            public void onComplete(String data) {
                                modeServerFullData = ModeServerFullData.analysis(data);
                                if (modeServerFullData != null) {
                                    e.onNext(modeServerFullData);
                                } else {
                                    loadingDialog.dismiss();
                                    LogUtils.e("未获取到表单审批数据！");
                                    ErrorDialog errorDialog = new ErrorDialog(getContext(),"未获取到表单审批数据");
                                    errorDialog.show();
                                }
                            }
                            @Override
                            public void onError(String data) {
                                e.onError(new RuntimeException(data));
                            }
                        });
                    } else {
                        e.onError(new RuntimeException("加载审批表单失败，表单包含和获取数据接口不可为空！"));
                    }
                }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MyObserver<ModeServerFullData>() {
                    @Override
                    public void onNext(ModeServerFullData serverFullData) {
                        loadingDialog.dismiss();
                        if (serverFullData != null) {
                            setViewData();
                        } else {
                            ToastUtils.showLong("未获取到数据");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        loadingDialog.dismiss();
                        ErrorDialog errorDialog = new ErrorDialog(getContext(),e.getMessage());
                        errorDialog.show();

                    }
                });
    }

    private void setViewData(){
        formName.setText(modeServerFullData.formName);
        formId.setText(String.valueOf(modeServerFullData.launchedForm.id));
        copyFormId.setOnClickListener(v -> {
            ClipboardUtils.copyText(String.valueOf(modeServerFullData.launchedForm.id));
            ToastUtils.showLong("已拷贝：" + modeServerFullData.launchedForm.id);
        });

        loadFullProcessDataView.setData(modeServerFullData, new IWaitLoadCompleteListener() {
            @Override
            public void onComplete() {
                //审批流程加载完毕加载字段数据
                loadFullDataView.setData(modeServerFullData,iHandlerLoadFormFull);
                loadApprovalView.setData(modeServerFullData, iHandlerLoadFormFull,new IWritePingJiaCallBack() {
                    @Override
                    public void onSuccessPl(long a, ModeFormFullApprovePerson formFullApprovePerson) {
                        loadFullProcessDataView.refreshAddData(a,formFullApprovePerson);
                    }

                    @Override
                    public void onSuccessSp() {
                        if (processViewApproveOver != null) processViewApproveOver.onOver();
                    }
                });

                formFullStata.setVisibility(VISIBLE);
                if (modeServerFullData.launchedForm.state == FormAddProcessFragment.SHEN_PI_STATE_ZHENG_ZAI_SHEN) {
                    formFullStata.setImageResource(R.mipmap.approve_state_ing);
                } else if (modeServerFullData.launchedForm.state == FormAddProcessFragment.SHEN_PI_STATE_TONG_GUO) {
                    formFullStata.setImageResource(R.mipmap.approve_state_tong_guo);
                } else if (modeServerFullData.launchedForm.state == FormAddProcessFragment.SHEN_PI_STATE_JU_JUE) {
                    formFullStata.setImageResource(R.mipmap.approve_state_bo_hui);
                }
            }
        });
    }

    public interface IProcessViewApproveOver {
        void onOver();
    }
}
